package com.icexxx.ice_browser;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.UnsupportedEncodingException;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.MouseInputAdapter;

import cn.hutool.core.swing.clipboard.ClipboardUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;

public class EventManager {

	public static MouseListener copyUrlButtonClick() {
		MouseListener mouseListener = new MouseInputAdapter() {
			public void mouseClicked(MouseEvent e) {
				JComponent jComponent = Context.get("requestAddressEditText");
				JTextField editText = (JTextField) jComponent;
				String text = editText.getText();
				ClipboardUtil.setStr(text);
			}
		};
		return mouseListener;
	}

	public static MouseListener copyContentButtonClick() {
		MouseListener mouseListener = new MouseInputAdapter() {
			public void mouseClicked(MouseEvent e) {
				JTextArea textArea = Context.getJTextArea("centerTextArea");
				String text = textArea.getText();
				ClipboardUtil.setStr(text);
			}
		};
		return mouseListener;
	}

	public static MouseListener parseUrlButtonClick() {
		MouseListener mouseListener = new MouseInputAdapter() {
			public void mouseClicked(MouseEvent e) {
				JTextField textField = Context.getJTextField("requestAddressEditText");
				String str = ClipboardUtil.getStr();
				textField.setText(str);
			}
		};
		return mouseListener;
	}

	public static MouseListener requestCommitButtonClick() {
		MouseListener mouseListener = new MouseInputAdapter() {
			public void mouseClicked(MouseEvent e) {
				JTextField textField = Context.getJTextField("requestAddressEditText");
				JTextArea paramTextArea = Context.getJTextArea("paramTextArea");
				JTextArea textArea = Context.getJTextArea("centerTextArea");
				JList<String> list = Context.getJList("list");
				JComboBox<String> requestMethodList = Context.getJComboBox("requestMethodList");
				Object selectedItem = requestMethodList.getSelectedItem();
				String selectedText = (String) selectedItem;
				String url = textField.getText();
				String param = paramTextArea.getText();
				String content = textArea.getText();
				String basePath = DataContext.get("basePath");
				url = addPrefixIfNot(url, "/");
				String key = keyText(url, basePath);
				int selectedIndex = list.getSelectedIndex();
				Color background = null;
				Color foreground = null;
				if (selectedIndex == -1) {
					ListManager.addItem(list, key);
					selectedIndex = 0;
					list.setSelectedIndex(selectedIndex);
					background = createColor(selectedIndex);
				} else {
					ListManager.addItem(list, key);
					selectedIndex = 0;
					list.setSelectedIndex(selectedIndex);
					background = createColor(selectedIndex);
				}
				String md5 = calcId(list);
				SelectedItemContext.setId(md5);
				SelectedItemContext.setColor(background);
				SelectedItemContext.setForegroundColor(foreground);
				url = wrapUrl(basePath, url);
				textField.setText(url);
				DataManager.save(selectedText, url, param, content, key, background);
			}
		};
		return mouseListener;
	}

	private static String addPrefixIfNot(String url, String string) {
		if (!url.startsWith("http://")) {
			url = StrUtil.addPrefixIfNot(url, "/");
		}
		return url;
	}

	private static Color createColor(int index) {
		Color color = SelectedItemContext.getColor();
		if (color == null) {
			return ColorManager.getFirstColor();
		}
		color = ColorManager.getNextColor(color);
		return color;
	}

	private static String keyText(String url, String basePath) {
		if (url.startsWith(basePath)) {
			url = StrUtil.removePrefix(url, basePath);
		}
		if (url.contains("?")) {
			url = StrUtil.subBefore(url, "?", false);
		}
		return url;
	}

	private static String wrapUrl(String basePath, String url) {
		if (url.startsWith(basePath)) {
			return url;
		}
		if (!url.startsWith("/")) {
			url = "/" + url;
		}
		url = basePath + url;
		return url;
	}

	public static MouseListener jListClick() {
		MouseListener mouseListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					Object source = e.getSource();
					@SuppressWarnings("unchecked")
					JList<String> jlist = (JList<String>) source;
					int selectedIndex = jlist.getSelectedIndex();
					SelectedItemContext.setIndex(selectedIndex);
					String id = calcId(jlist);
					SelectedItemContext.setId(id);
					Request request = DataCenter.findById(id);
					String url = request.getUrl();
					String param = request.getParam();
					String method = request.getMethod();
					String content = request.getContent();
					JTextField textField = Context.getJTextField("requestAddressEditText");
					JTextArea paramTextArea = Context.getJTextArea("paramTextArea");
					JTextArea textArea = Context.getJTextArea("centerTextArea");
					JComboBox<String> requestMethodList = Context.getJComboBox("requestMethodList");
					textField.setText(url);
					int SelectedIndex = ListManager.calcSelectedIndex(method);
					requestMethodList.setSelectedIndex(SelectedIndex);
					textField.setText(url);
					paramTextArea.setText(param);
					textArea.setText(content);
				}
			}
		};
		return mouseListener;
	}

	private static String calcId(JList<String> jlist) {
		String selectedValue = jlist.getSelectedValue();
		byte[] serialize = ObjectUtil.serialize(selectedValue);
		String md5 = null;
		try {
			md5 = SecureUtil.md5(new String(serialize, "UTF-8"));
			return md5;
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		return null;
	}
}
